<cfparam name="Program_Year" type="string" default="#request.currentProgramYear#">

<cfscript>
	base = new assets.csac.com.Base();
	orderGateway = new assets.csac.com.gateway.OrderGateway();
	orders = orderGateway.query(Deleted = 0, Program_Year = Program_Year);
	orderValueGateway = new assets.csac.com.gateway.OrderValueGateway();
	orderValues = base.QueryToStruct(orderValueGateway.query(Active = 1));
</cfscript>

<cfoutput>
<cfloop query="orders">
	<cfset qtys = ListToArray(Quantities) />
	<cfset orderValueIDs = ListToArray(Order_Value_IDs) />
	<cfif ArrayLen(qtys) eq ArrayLen(orderValueIDs)>
		<cfloop index="i" from="1" to="#ArrayLen(orderValues)#">
			<cfset index = ArrayFind(orderValueIDs, orderValues[i].Order_Value_ID) />
			<cfif index eq 0>
				<cfset qty = 0 />
				<cfdump var="#orders.order_id[recordcount]#">
			<cfelse>
				<cfset qty = Val(qtys[index]) />
			</cfif>
			<cfif NOT StructKeyExists(orderValues[i], "Quantity_Approved")>
				<cfset orderValues[i].Quantity_Approved = 0 />
			</cfif>
			<cfif NOT StructKeyExists(orderValues[i], "Quantity_Unapproved")>
				<cfset orderValues[i].Quantity_Unapproved = 0 />
			</cfif>
			<cfif NOT StructKeyExists(orderValues[i], "Quantity_Total")>
				<cfset orderValues[i].Quantity_Total = 0 />
			</cfif>
			<cfif Approved>
				<cfset orderValues[i].Quantity_Approved = orderValues[i].Quantity_Approved + qty />
			<cfelse>
				<cfset orderValues[i].Quantity_Unapproved = orderValues[i].Quantity_Unapproved + qty />
			</cfif>
			<cfset orderValues[i].Quantity_Total = orderValues[i].Quantity_Total + qty />		
		</cfloop>
	</cfif>
</cfloop>

<h1>Order Quantities Summary for #Program_Year#</h1>

<table class="table table-striped table-bordered">
	<thead>
		<tr>
			<th>SKU</th>
			<th>Item</th>
			<th>Approved</th>
			<th>Unapproved</th>
			<th>Total</th>
		</tr>
	</thead>
	<tbody>
		<cfloop index="i" from="1" to="#ArrayLen(orderValues)#">
			<tr>
				<td>#orderValues[i].SKU#</td>
				<td>#orderValues[i].Description#</td>
				<td align="right">#NumberFormat(orderValues[i].Quantity_Approved)#</td>
				<td align="right">#NumberFormat(orderValues[i].Quantity_Unapproved)#</td>
				<td align="right">#NumberFormat(orderValues[i].Quantity_Total)#</td>
			</tr>
		</cfloop>
	</tbody>
</table>
</cfoutput>


<!--- <cfdump var="#orderValues#"> --->

<!--- <cfset orderValues = ArrayNew(1) />
<cfloop query="orderValues">
	<cfset i = RecordCount />
	<cfset orderValues[i] = StructNew() />
	<cfloop index="col" list="#orderValues.Column_List#">
		<cfset orderValues[i][col] = orderValues[col][i] />
	</cfloop>
</cfloop> --->


<!--- <!--- Role params --->
<cfparam name="organizationID" type="integer" default="0">
<cfparam name="regionID" type="integer" default="0">
<!--- Sorting params --->
<cfparam name="orderBy" type="string" default="#client.ordersOrderBy#">
<cfparam name="orderDir" type="string" default="#client.ordersOrderDir#">
<!--- Filter params --->
<cfparam name="filterOrganizationIDs" type="string" default="#client.ordersOrganizationIDs#">
<cfparam name="filterRegionIDs" type="string" default="#client.ordersRegionIDs#">
<cfparam name="filterPBS" type="string" default="#client.ordersPBS#">
<cfparam name="filterApproved" type="string" default="#client.ordersApproved#">
<cfparam name="filterPeriod" type="string" default="#client.ordersPeriod#">
<cfparam name="filterFromDate" type="string" default="#client.ordersFromDate#">
<cfparam name="filterToDate" type="string" default="#client.ordersToDate#">
<cfparam name="filterField" type="string" default="#client.ordersField#">
<cfif filterPeriod neq "Date">
	<cfset filterFromDate = "">
	<cfset filterToDate = "">
</cfif>

<!--- Checkboxes --->
<cfparam name="orderIDs" type="string" default="">

<cfset orders = application.orderGateway.getAllAsQuery("*", organizationID, regionID, orderBy, orderDir, filterOrganizationIDs, filterRegionIDs, filterPBS, filterApproved, 0, filterPeriod, filterFromDate, filterToDate, filterField)>

<cfoutput>

	<div class="buttonBar">
		<a href="#script_name#?ui=orders" class="formLink2">Back</a>
	</div>
	
	<cfif orders.RecordCount gt 0>

		<cfset orderValues = application.orderGateway.getOrderValues()>
		<cfloop query="orderValues">
			<cfif Order_Value_ID neq 15>
				<cfset itemStruct = StructNew()>
				<cfset itemStruct.orderValueID = Order_Value_ID>
				<cfset itemStruct.approvedCount = 0>
				<cfset itemStruct.unapprovedCount = 0>
				<cfset "item_#Order_Value_ID#_struct" = Duplicate(itemStruct)>
			</cfif>
		</cfloop>
		
		<cfset regions = application.countyGateway.getRegions()>
		<cfloop query="regions">
			<cfset regionStruct = StructNew()>
			<cfset regionStruct.regionID = Region_ID>
			<cfset regionStruct.approvedCount = 0>
			<cfset regionStruct.unapprovedCount = 0>
			<cfset regionStruct.studentCount = 0>
			<cfset regionStruct.volunteerCount = 0>
			<cfset "region_#Region_ID#_struct" = Duplicate(regionStruct)>
		</cfloop>
		
		<cfset totalStudents = 0>
		<cfset totalVolunteers = 0>
		
		<cfloop query="orders">
			<cfset isApproved = orderApproved>
			<cfset totalStudents = totalStudents + Student_Count>
			<cfset totalVolunteers = totalVolunteers + Volunteer_Count>
			<!--- <cfset studentCount = Student_Count>
			<cfset volunteerCount = Volunteer_Count> --->
			<cfwddx action="wddx2cfml" input="#Order_Data#" output="orderData">
			<cfloop query="orderData">
				<cfif orderValueID neq 15 and quantity gt 0>
					<cfif isDefined("item_" & orderValueID & "_struct")>
						<cfset itemStruct = Duplicate(Evaluate("item_" & orderValueID & "_struct"))>
						<cfif isApproved>
							<cfset itemStruct.approvedCount = itemStruct.approvedCount + quantity>
						<cfelse>
							<cfset itemStruct.unapprovedCount = itemStruct.unapprovedCount + quantity>
						</cfif>
						<cfset "item_#orderValueID#_struct" = Duplicate(itemStruct)>
					</cfif>
				</cfif>
			</cfloop>
			<cfset regionStruct = Duplicate(Evaluate("region_" & Region_ID & "_struct"))>
			<cfif isApproved>
				<cfset regionStruct.approvedCount = regionStruct.approvedCount + 1>
			<cfelse>
				<cfset regionStruct.unapprovedCount = regionStruct.unapprovedCount + 1>
			</cfif>
			<cfset regionStruct.studentCount = regionStruct.studentCount + Student_Count>
			<cfset regionStruct.volunteerCount = regionStruct.volunteerCount + Volunteer_Count>
			<cfset "region_#Region_ID#_struct" = Duplicate(regionStruct)>
			
		</cfloop>
		
		<h2>Orders by item</h2>
		
		<div class="h2Content">
		
			<table class="list">
				<tr>
					<th>Item No.</th>
					<th>Item</th>
					<th style="width: 70px;">Approved</th>
					<th style="width: 70px;">Unapproved</th>
					<th style="width: 70px;">Total</th>
				</tr>
				<cfset approvedCount = 0>
				<cfset unapprovedCount = 0>
				<cfset totalCount = 0>
				<cfloop query="orderValues">
					<cfif Order_Value_ID neq 15>
						<cfif IsDefined("item_#Order_Value_ID#_struct")>
							<cfset itemStruct = Duplicate(Evaluate("item_" & Order_Value_ID & "_struct"))>
							<cfset itemCount = itemStruct.approvedCount + itemStruct.unapprovedCount>
							<cfset approvedCount = approvedCount + itemStruct.approvedCount>
							<cfset unapprovedCount = unapprovedCount + itemStruct.unapprovedCount>
							<cfset totalCount = totalCount + itemCount>
						<cfelse>
							<cfset totalCount = 0>
						</cfif>
						<tr<cfif currentRow mod 2 eq 0> class="evenRow"<cfelse> class="oddRow"</cfif>>
							<td>#SKU#</td>
							<td>#Description#</td>
							<cfif totalCount gt 0>
								<td align="right">#NumberFormat(itemStruct.approvedCount)#</td>
								<td align="right">#NumberFormat(itemStruct.unapprovedCount)#</td>
								<td align="right">#NumberFormat(itemCount)#</td>
							<cfelse>
								<td align="right">0</td>
								<td align="right">0</td>
								<td align="right">0</td>
							</cfif>
						</tr>
					</cfif>	
				</cfloop>
				<tr>
					<td colspan="2"><strong>Totals</strong></td>
					<td align="right"><strong>#NumberFormat(approvedCount)#</strong></td>
					<td align="right"><strong>#NumberFormat(unapprovedCount)#</strong></td>
					<td align="right"><strong>#NumberFormat(totalCount)#</strong></td>
				</tr>
			</table>
		
		</div>
		
		<h2>Orders by region</h2>
		
		<div class="h2Content">
		
			<table class="list">
				<tr>
					<th>Region ID</th>
					<th>Region</th>
					<th style="width: 70px;">Approved</th>
					<th style="width: 70px;">Unapproved</th>
					<th style="width: 70px;">Total</th>
					<th style="width: 70px;">Students</th>
					<th style="width: 70px;">Volunteers</th>
				</tr>
				<cfset totalApproved = 0>
				<cfset totalUnapproved = 0>
				<cfset totalCount = 0>
				<cfloop query="regions">
					<cfif IsDefined("region_#Region_ID#_struct")>
						<cfset regionStruct = Duplicate(Evaluate("region_" & Region_ID & "_struct"))>
						<cfset regionCount = regionStruct.approvedCount + regionStruct.unapprovedCount>
						<cfset totalApproved = totalApproved + regionStruct.approvedCount>
						<cfset totalUnapproved = totalUnapproved + regionStruct.unapprovedCount>
						<cfset totalCount = totalCount + regionCount>
					<cfelse>
						<cfset regionCount = 0>
					</cfif>
					<tr<cfif currentRow mod 2 eq 0> class="evenRow"<cfelse> class="oddRow"</cfif>>
						<td>#Region_ID#</td>
						<td>#Region#</td>
						<cfif regionCount gt 0>
							<td align="right">#NumberFormat(regionStruct.approvedCount)#</td>
							<td align="right">#NumberFormat(regionStruct.unapprovedCount)#</td>
							<td align="right">#NumberFormat(regionCount)#</td>
						<cfelse>
							<td align="right">0</td>
							<td align="right">0</td>
							<td align="right">0</td>
						</cfif>
						<td align="right">#NumberFormat(regionStruct.studentCount)#</td>
						<td align="right">#NumberFormat(regionStruct.volunteerCount)#</td>
					</tr>	
				</cfloop>
				<tr>
					<td colspan="2"><strong>Totals</strong></td>
					<td align="right"><strong>#NumberFormat(totalApproved)#</strong></td>
					<td align="right"><strong>#NumberFormat(totalUnapproved)#</strong></td>
					<td align="right"><strong>#NumberFormat(totalCount)#</strong></td>
					<td align="right"><strong>#NumberFormat(totalStudents)#</strong></td>
					<td align="right"><strong>#NumberFormat(totalVolunteers)#</strong></td>
				</tr>
			</table>
			
		</div>
		
	</cfif>

	<cfif orders.RecordCount eq 0>
	
		<p class="noRecords">(No orders available to report)</p>
		
	</cfif>
	
	<p style="font-size: 10px;">Report generated #DateFormat(now(), "m/d/yyyy")# #TimeFormat(now(), "h:mmtt")#</p>

</cfoutput> --->